package com.zjitc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjitc.model.CartItem;
import com.zjitc.model.Orderitem;
import com.zjitc.model.Orders;
import com.zjitc.model.User;
import com.zjitc.service.CartService;
import com.zjitc.service.OrderService;
import com.zjitc.util.CommonUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class OrderController {

  @Autowired
  CartService cartService;

  @Autowired
  OrderService orderService;

  @RequestMapping("/createMyOrder")
  public String createMyOrder(HttpSession session) {
    User user = (User) session.getAttribute("user");
    //2.生成一个订单对象 order（id UUID 时间 未付款 收货人信息（默认收货信息，是当前用户） 用户）
    Orders orders = new Orders();
    orders.setOid(CommonUtils.getUUID());
    orders.setAddr(user.getAddr());
    orders.setName(user.getName());
    orders.setPhone(user.getPhone());
    orders.setState(0); //0：代表未付款。   1：付款，但未发货。  2：收货 交易完成   3：已退款
    orders.setOrdertime(CommonUtils.dateToString(new Date()));

    List<CartItem> list = cartService.findByUid(user.getUid());
    //4.根据遍历cartlist购物车条目信息，生成订单条目（订单总价格total）
    List<Orderitem> orderitemList = new ArrayList<>();

    Double total = 0.0;
    for (CartItem item : list) {
      Orderitem orderitem = new Orderitem();
      orderitem.setOrder(orders);
      orderitem.setProduct(item.getProduct());
      orderitem.setCount(item.getQuantity());
      orderitem.setOsatate(0);
      orderitem.setSubtotal(item.getProduct().getShop_price() * item.getQuantity());
      total = total + item.getProduct().getShop_price() * item.getQuantity();
      orderitemList.add(orderitem);
    }
    orders.setTotal(total);
    orders.setOrderitemList(orderitemList);

    orderService.add(orders);
    session.setAttribute("orders", orders);
    return "/WEB-INF/user/order.jsp";
  }

  @RequestMapping("/findMyOrder")
  private String findMyOrder(
      @RequestParam(name = "start", required = false, defaultValue = "1") int start,
      @RequestParam(name = "count", required = false, defaultValue = "10") int count,
      HttpSession session, Integer uid, HttpServletRequest request) {
    PageHelper.startPage(start, count);
    List<Orders> list = orderService.findPageByUid(uid);
    PageInfo<Orders> pageInfo = new PageInfo<>(list);
    session.setAttribute("list", list);
    session.setAttribute("uid", uid);
    session.setAttribute("page", pageInfo);
    return "/WEB-INF/user/orderList.jsp";
  }

  @RequestMapping("/updateOrder")
  private String updateOrder(HttpSession session, String name, String phone, String addr) {
    Orders order = (Orders) session.getAttribute("orders");
    orderService.updateOrder(order.getOid(), name, phone, addr);
    String total = String.valueOf(order.getTotal());
    session.setAttribute("WIDout_trade_no", order.getOid());
    session.setAttribute("WIDtotal_amount", total);
    session.setAttribute("WIDsubject", "test");
    session.setAttribute("WIDbody", "test");

    return "/WEB-INF/user/alipay/alipay.trade.page.pay.jsp";
  }

  @RequestMapping("deleteOrder")
  public String deleteOrder(String oid,HttpSession session){
    orderService.deleteOrder(oid);
    Integer uid= (Integer) session.getAttribute("uid");
    return "redirect:/findMyOrder.do?uid="+uid;

  }

}
